layui.use(['form', 'table', 'layer', 'common', 'element'], function () {
    let common = layui.common;
    let form = layui.form;
    let table = layui.table;
    let layer = layui.layer;
    let element = layui.element;
    let $ = layui.$;

    /**
     * 正常
     */
    table.render({
        elem: "#goodsList"
        , id: 'goodsList'
        , method: 'get'
        , url: '/goods/sgc'
        , page: ({
            layout: ['prev', 'page', 'next', 'count']
            , first: '首页'
            , last: '尾页'
            , curr: 1
            , groups: 6
        })
        , cols: [[
            {title: '菜品分类', field: 'sgc_name', fixed: 'left'}
            , {title: '权重', field: "sgc_sort"}
            , {title: '操作', toolbar: "#operation"}
        ]]
        , limit: 20
        , event: true
        , loading: true
    });

    table.on("tool(goodsList)", function (obj) {
        let event_name = obj.event;
        let id = $(this).data('id');
        if (event_name === 'edit'){
            form.val("editStoreClass",{
                id: id,
                sgc_name: obj.data['sgc_name'],
                sgc_sort: obj.data['sgc_sort']
            });
            layer.open({
                type: 1,
                title: '修改菜品分类信息',
                area: 'auto',
                shadeClose: true,
                content: $("#editStoreGoodsClassDiv")
            });
        }
        if (event_name === 'del'){
            $.ajax({
                url: '/goods/del_store_goods_class',
                type: 'post',
                data: {
                    data: {
                        sgc_id: id
                    }
                },
                dataType: 'json',
                success: function (res) {
                    if (res.code === 0){
                        common.tipMessage(res.msg, 6, function () {
                            table.reload('goodsList', {
                                method: "get"
                            });
                        });
                    }else {
                        common.tipMessage(res.msg);
                    }
                }
            });
        }
    });

    $("#addStoreGoodsClassBtn").click(function () {
        layer.open({
            type: 1,
            title: '添加菜品分类',
            area: 'auto',
            shadeClose: true,
            content: $("#addStoreGoodsClassDiv")
        });
    });

    form.on("submit(addStoreGoodsClass)", function (data) {
        $.ajax({
            url: '/goods/add_store_goods_class',
            type: 'post',
            dataType: 'json',
            data: {
                _token: data.field['_token'],
                data: {
                    sgc_name: data.field['sgc_name'],
                    sgc_sort: data.field['sgc_sort']
                }
            },
            success: function (res) {
                if (res.code === 0){
                    common.tipMessage(res.msg, 6, function () {
                        table.reload('goodsList', {
                            method: 'get'
                        });
                    });
                }else {
                    common.tipMessage(res.msg);
                }
            }
        });
        return false;
    });

    form.on("submit(editStoreGoodsClass)", function (data) {
        $.ajax({
            url: '/goods/edit_store_goods_class',
            type: 'post',
            dataType: 'json',
            data: {
                id: data.field['id'],
                _token: data.field['_token'],
                data: {
                    sgc_name: data.field['sgc_name'],
                    sgc_sort: data.field['sgc_sort']
                }
            },
            success: function (res) {
                if (res.code === 0){
                    common.tipMessage(res.msg, 6, function () {
                        table.reload('goodsList', {
                            method: 'get'
                        });
                    });
                }else {
                    common.tipMessage(res.msg);
                }
            }
        });
        return false;
    });
});
